package com.waylau.spring.boot.blog.service;

import java.util.*;

import javax.transaction.Transactional;

import com.waylau.spring.boot.blog.domain.Authority;
import com.waylau.spring.boot.blog.domain.Blog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.waylau.spring.boot.blog.domain.User;
import com.waylau.spring.boot.blog.repository.UserRepository;

/**
 * User 服务.
 * 
 * @since 1.0.0 2017年3月18日
 * @author <a href="https://waylau.com">Way Lau</a>
 */
@Service
public class UserServiceImpl implements UserService, UserDetailsService {

	@Autowired
	private UserRepository userRepository;



	@Transactional
	@Override
	public User saveUser(User user) {
		if(user.getAuthorities()!=null && user.getAuthorities().size()<=0){
			Authority authority = new Authority();
			authority.setId((long) 2);
			List<Authority> authorities = new ArrayList<>();
			authorities.add(authority);
			user.setAuthorities(authorities);
		}
		return userRepository.save(user);
	}

	@Transactional
	@Override
	public void removeUser(Long id) {
		userRepository.delete(id);
	}

	@Transactional
	@Override
	public void removeUsersInBatch(List<User> users) {
		userRepository.deleteInBatch(users);
	}
	
	@Transactional
	@Override
	public User updateUser(User user) {
		return userRepository.save(user);
	}

	@Override
	public User getUserById(Long id) {
		return userRepository.getOne(id);
	}

	@Override
	public User getUserByUsername(String name) {
		return userRepository.findByUsername(name);
	}

	@Override
	public List<User> listUsers() {
		return userRepository.findAll();
	}

	@Override
	public Page<User> listUsersByNameLike(String name, Pageable pageable) {
		// 模糊查询
		name = "%" + name + "%";
		Page<User> users = userRepository.findByNameLike(name, pageable);
		return users;
	}

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		return userRepository.findByUsername(username);
	}

	@Override
	public List<User> listUsersByUsernames(Collection<String> usernames) {
		return userRepository.findByUsernameIn(usernames);
	}

	@Override
	public void follow(String followName) {
		User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		User follow = userRepository.findByUsername(followName);
		Boolean flag = false;
		for (User u : user.getUsers()) {
			if(followName.equals(u.getUsername())){
				flag = true;
				break;
			}
		}
		if(!flag){
			user.getUsers().add(follow);
		}
		userRepository.save(user);
	}

	@Override
	public void cancelFollow(Long followId) {
		User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		User follow = userRepository.findOne(followId);
		Set<User> users = new HashSet<User>();
		user.getUsers().forEach(a->{
			if(!a.getId().equals(followId)){
				users.add(a);
			}
		});
		user.setUsers(users);
		userRepository.save(user);
	}

	@Override
	public Page<User> listUsersByIds(Collection<Long> ids, Pageable pageable) {
		Page<User> users = userRepository.findByIdIn(ids,pageable);
		return users;
	}

	@Override
	public User checkUserByUserNameAndEmail(String name, String emil) {
		return userRepository.findByUsernameAndEmail(name,emil);
	}
}
